/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bai2nio;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

class Inout{
    public void read(String name){
        try (BufferedReader br = new BufferedReader(new FileReader(name))){
            String sCurrentLine;
            String s= "";
		while ((sCurrentLine = br.readLine()) != null) {
                    s+=sCurrentLine;
                    System.out.println(sCurrentLine);
                }
                System.out.println(s);
	} 
        catch (IOException e){
	} 
        
    }
    
    public void write(String name1,String name2){
        try{
            String content = name1;
		File file = new File(name2);
 		if (!file.exists()) {
                    file.createNewFile();
	}
 
	FileWriter fw = new FileWriter(file.getAbsoluteFile());
        try (BufferedWriter bw = new BufferedWriter(fw)) {
            bw.write(content);
        }
 	System.out.println("Done");
 	} 
        catch (IOException e){
	}
    }

    public void getInfor(String name){   
        
        String files;
        File folder = new File(name);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++)
        {

            if (listOfFiles[i].isFile())
            {
                files = listOfFiles[i].getName();
                
                    System.out.println(files);
                
            }
        }
    }
}

class MP3 {
    private Player player;
    private String filename;
    
    public MP3(String filename) {
        this.filename = filename;
    }
    
    public void stop() {
        if (player != null)
            player.close();
    }
    
    public void play() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
            player = new Player(bis);
        } catch (FileNotFoundException | JavaLayerException ex) {
            System.out.println(ex);
        }
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    player.play();
                } catch (Exception ex) {
                    System.out.println(ex);
                }
            }
        }).start();
    }
}


public class Bai2nio {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Inout ngoc= new Inout();
        ngoc.read("example.txt");
        ngoc.write("example.txt", "ngocquang.txt");
        ngoc.getInfor("ngoc");
        MP3 ngocmp3 = new MP3("ngoc\\tranquangngoc.mp3");
        ngocmp3.play();
    }
}
